home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume9 / pep / part04 < prev    next >
Encoding:
Text File  |  1989-12-29  |  23.2 KB  |  644 lines

  1. Newsgroups: comp.sources.misc
  2. organization: gisle@ifi.uio.no
  3. subject: v09i095: PEP filter program [ part 4 of 5 ]
  4. from: gisle@ifi.uio.no (Gisle Hannemyr)
  5. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  6.  
  7. Posting-number: Volume 9, Issue 95
  8. Submitted-by: gisle@ifi.uio.no (Gisle Hannemyr)
  9. Archive-name: pep/part04
  10.  
  11. # This is a shell archive [ part 4 of of 5 ]
  12. # Remove everything above and including the cut line.
  13. # Then run the rest of the file through /bin/sh (not csh).
  14. #--cut here-----cut here-----cut here-----cut here-----cut here-----cut here--#
  15. #!/bin/sh
  16. # shar: Shell Archiver
  17. # Execute the following text with /bin/sh to create the file(s):
  18. #    main.c
  19. # This archive created: Fri Dec 29 14:42:45 1989
  20. # Wrapped by: Gisle Hannemyr (gisle@ifi.uio.no)
  21. echo shar: extracting main.c
  22. sed 's/^XX//' << \SHAR_EOF > main.c
  23. XX/* main.c  1989 december 28 [gh]
  24. XX+-----------------------------------------------------------------------------
  25. XX| Abstract:
  26. XX|    General purpose filter and file cleaning program.  9 out 10 hackers
  27. XX|    prefer pep to any other soap. It is named after an excellent Norwegian
  28. XX|    detergent.
  29. XX|
  30. XX| Authorship:
  31. XX|    Copyright (c) 1988, 1989 Gisle Hannemyr.
  32. XX|    Permission is granted to hack, make and distribute copies of this program
  33. XX|    as long as this notice and the copyright notices are not removed.
  34. XX|    If you intend to distribute changed versions of this program, please make
  35. XX|    an entry in the "history" log (below) and mark the hacked lines with your
  36. XX|    initials. I maintain the program, and shall appreiciate copies of bug
  37. XX|    fixes and new versions.
  38. XX|    Flames, bug reports, comments and improvements to:
  39. XX|       snail: Gisle Hannemyr, Brageveien 3A, 0452 Oslo, Norway
  40. XX|       email: EAN:  gisle@nr.uninett
  41. XX|              Inet: gisle@ifi.uio.no
  42. XX|              UUCP: ...!mcvax!ifi!gisle
  43. XX|              (and several BBS mailboxes in the Oslo area).
  44. XX|
  45. XX| Acknowledgments:
  46. XX|    SYS V.2 rename courtesy of Robert Andersson (ra@isncr.is.se)
  47. XX|    VMS rename courtesy of Bjorn Larsen.
  48. XX|    Thanks to Inge Arnesen for finding & fixing a bug, (and to Nils-Eivind
  49. XX|    Naas for bringing it to my attention).
  50. XX|
  51. XX| History:
  52. XX|    2.1 29 dec 89 [gh] Fixed pipe bug, added -v option, misc. speedups
  53. XX|    2.0 22 jan 89 [gh] Made it a filter, environment lookup, fixed pathbug.
  54. XX|    1.6  7 nov 88 [gh] Added ANSI interpretation.
  55. XX|    1.5  6 aug 88 [gh] Hacked it to not clobber original date.
  56. XX|    1.4  7 jul 88 [gh] Added general purpose conversion table.
  57. XX|    1.3 13 nov 87 [gh] Fixed find first so it works on true blue too.
  58. XX|    1.2 11 nov 87 [gh] Compensated for Turbo-C bug (isspace > 128 is bogus)
  59. XX|    1.1 31 aug 87 [gh] Added VMS.
  60. XX|    1.0 30 aug 87 [gh] Wrote it.
  61. XX|
  62. XX| Portability:
  63. XX|    So far, PEP has been tested under CP/M, MS-DOS, BSD and VMS.
  64. XX|    The implementation dependencies are:
  65. XX|      * How the compiler identify itself and the operating system.
  66. XX|      * How microcomputer compilers simulate the UNIX text line terminator.
  67. XX|      * How operating systems folds command lines (i.e. CP/M folds).
  68. XX|      * How operating systems expands command line wildcards (ie. UNIX does).
  69. XX|
  70. XX|    This dependencies are implemented using IFDEFs. You should make sure that
  71. XX|    exactly one of the following symbols (macro names) are defined:
  72. XX|     * __CPM86__  -- For CP/M, MP/M, C-DOS and derivatives
  73. XX|     * __MSDOS__  -- For MS-DOS and derivatives
  74. XX|     * __UNIX__   -- For BSD UNIX (SYS V.3 and generic too?)
  75. XX|     * __VMS__    -- For VMS
  76. XX|
  77. XX|    In addition, the following symbols may be twiddled if desired:
  78. XX|     * STRICMP    -- Define this if linker complains about missing "stricmp"
  79. XX|     * SYSV2      -- For SYS V.2 UNIX    (if no "rename" in standard lib)
  80. XX|     * __TURBOC__ -- For Borlands TURBOC (undefine it if you want PEP to
  81. XX|                     change the date on the files it filters).
  82. XX|     * VMSV1      -- For VAX C V.1.x VMS (if no "rename" in standard lib)
  83. XX|
  84. XX|    Most compilers already predefines a macro that identify the target oper-
  85. XX|    ating system.  Unfortunately, different vendors uses slightly different
  86. XX|    symbols.  Please add to the section headlined  "canonize predefined
  87. XX|    macroes" (below) if your compiler requires it.
  88. XX|
  89. XX|    Note: PEP makes some assumptions about standard headers.  See the file
  90. XX|          "header.txt" for details.
  91. XX|
  92. XX|    Btw. -- these are the compilers I have used to make pep:
  93. XX|     * MS-DOS,    Microsoft C ver. 5.1
  94. XX|           Turbo C ver. 2.0
  95. XX|     * BSD UNIX,  SunOS Release 4.0
  96. XX|     * SVID UNIX, NCR Tower System V.2
  97. XX|     * VMS,       VAX C release 2.4
  98. XX|
  99. XX| Environment:
  100. XX|    PEP -- should point to directory with conversion tables.
  101. XX|
  102. XX| Bugs:
  103. XX|    * I do not recompile PEP on all operating systems at each release.
  104. XX|      Software rot may cause a particular version to need some tweaking.
  105. XX|      Please mail me if you experience this -- but please read the enclosed
  106. XX|      file "header.txt" first.
  107. XX+---------------------------------------------------------------------------*/
  108. XX
  109. XX
  110. XX/*---( Includes )-----------------------------------------------------------*/
  111. XX
  112. XX#define  MAIN
  113. XX
  114. XX#include <stdio.h>
  115. XX#include "pep.h"
  116. XX#include <string.h>
  117. XX#include <ctype.h>
  118. XX#ifdef __VMS__
  119. XX#include <types.h>
  120. XX#include <stat.h>
  121. XX#else
  122. XX#include <sys/types.h>
  123. XX#include <sys/stat.h>
  124. XX#endif
  125. XX#if __CPM86__ || __MSDOS__
  126. XX#include <dos.h>
  127. XX#include "bdmg.h"
  128. XX#endif
  129. XX#ifdef __MSDOS__
  130. XX#include <fcntl.h>
  131. XX#include <io.h>
  132. XX#endif
  133. XX#ifdef __UNIX__
  134. XX#include <malloc.h>
  135. XX#else
  136. XX#include <stdlib.h>
  137. XX#endif
  138. XX#ifdef VMSV1
  139. XX#include <descrip.h>
  140. XX#endif
  141. XX
  142. XX
  143. XX/*---( defines )------------------------------------------------------------*/
  144. XX
  145. XX#define VERSION     "2.1"        /* Version number.                  */
  146. XX#define TEMPFIL     "PEP.TMP"           /* Tempfile.                        */
  147. XX
  148. XX
  149. XX/*---( constants )----------------------------------------------------------*/
  150. XX
  151. XXstatic char about1[] = "\n\
  152. XXPep is a program that converts almost anything into plain text files.\n\
  153. XXPermission is granted to make and distribute copies of this program as\n\
  154. XXlong as the copyright and this notice appears; and that all the files\n\
  155. XXin the distribution is included.  ( The files in the distribution is\n\
  156. XXdocumented in the release note file \"aaread.me\". )\n\n";
  157. XX
  158. XXstatic char about2[] = "\
  159. XXBug reports, improvements, comments, suggestions and flames to:\n\
  160. XX   snail: Gisle Hannemyr, Brageveien 3A, 0452 Oslo, Norway\n\
  161. XX   email: EAN:  gisle@nr.uninett;\n\
  162. XX          Inet: gisle@ifi.uio.no;\n\
  163. XX          UUCP: ...!mcvax!ifi!gisle\n\
  164. XX          (and several BBS mailboxes).\n";
  165. XX
  166. XXstatic char usage1[] = "   Usage: pep [options] [filename ...]\n\
  167. XX   Valid options:\n\
  168. XX\t-a        -- about pep\n\
  169. XX\t-b        -- remove non ASCII-codes\n\
  170. XX\t-c[size]  -- compress spaces to tabs\n\
  171. XX\t-d+/-     -- convert to/from DEC 8 bit charset\n\
  172. XX\t-e[guard] -- interprete ANSI escape sequences\n\
  173. XX\t-g<file>  -- get conversion table from file\n\
  174. XX\t-h        -- print this quick summary\n\
  175. XX\t-i+/-     -- convert to/from IBM-PC charset\n\
  176. XX\t-k+/-     -- convert to/from \"Kman\" charset\n\
  177. XX\t-m+/-     -- convert to/from Macintosh charset\n";
  178. XX
  179. XXchar usage2[] = "\
  180. XX\t-o[b]     -- write output to named files\n\
  181. XX\t-p        -- display transformations and pause\n\
  182. XX\t-s[size]  -- extract strings\n\
  183. XX\t-t[size]  -- expand tabstops\n\
  184. XX\t-u<term>  -- use special line terminator\n\
  185. XX\t-v        -- terminate only paragraphs\n\
  186. XX\t-w+/-     -- convert to/from WS document mode\n\
  187. XX\t-x        -- expand non printing chars\n\
  188. XX\t-z        -- zero parity bit\n";
  189. XX
  190. XX
  191. XX/*---( variables )----------------------------------------------------------*/
  192. XX
  193. XXstatic int  cright = TRUE;                  /* Flag copyright undisplayed   */
  194. XXstatic int  guardl = 0;                     /* ANSI overwrite guard level   */
  195. XX
  196. XXlong LCount = 0L;                /* Global line count            */
  197. XXint  LineXx = 0;                /* Horisontal position on line. */
  198. XXint  ITabSz = 8;                /* Input  tabulator size.       */
  199. XXint  OTabSz = 8;                /* Output tabulator size.       */
  200. XXint  StrSiz = 4;                /* String size for strings.     */
  201. XX#if __UNIX__ || __VMS__
  202. XXint  EndOLn = '\n';                /* Under UNIX, default is LF;   */
  203. XX#else
  204. XXint  EndOLn = -1;                /* else, the default is CRLF.   */
  205. XX#endif
  206. XX
  207. XXstatic    int  backup = FALSE;                   /* Keep backup copy.            */
  208. XX    int  bflagb = FALSE;            /* Binary wash.                 */
  209. XX    int  cflagc = FALSE;            /* Compress                     */
  210. XX    int  dflagd = FALSE;            /* DEC character set.           */
  211. XXstatic    int  eflage = FALSE;            /* ANSI escape sequences.       */
  212. XX    int  gflagg = FALSE;            /* General fold table           */
  213. XX    int  iflagi = FALSE;            /* IBM character set.           */
  214. XX    int  kflagk = FALSE;            /* Kman character set.          */
  215. XX    int  mflagm = FALSE;            /* MAC character set.           */
  216. XX#ifdef __VMS__
  217. XXstatic    int  oflago = TRUE;            /* VMS has no pipes.        */
  218. XX#else
  219. XXstatic    int  oflago = FALSE;            /* Write output on files.        */
  220. XX#endif
  221. XXstatic    int  pflagp = FALSE;              /* Pause.                       */
  222. XX    int  sflags = FALSE;            /* String extraction.           */
  223. XX    int  tflagt = FALSE;            /* Tab expansion                */
  224. XXstatic    int  uflagu = FALSE;            /* Use special line terminator. */
  225. XX    int  vflagv = FALSE;            /* Terminate only paragraphs.   */
  226. XX    int  wflag0 = FALSE;            /* From WS doc. mode to 7-bit.  */
  227. XX    int  wflag1 = FALSE;            /* From 7-bit to WS doc. mode.  */
  228. XX    int  xflagx = FALSE;
  229. XX    int  zflagz = FALSE;
  230. XX
  231. XX
  232. XX/*---( housekeeping )-------------------------------------------------------*/
  233. XX
  234. XX
  235. XX/*
  236. XX| Abs: Write control or meta character ii on stderr using standard conventions.
  237. XX*/
  238. XXstatic void fputctl(ii)
  239. XXint ii;
  240. XX{
  241. XX   if ((ii < 0) || (ii > 255)) fputs("<BOGUS>",stderr);
  242. XX   else if (ii == 255) fputs("M-del",stderr);
  243. XX   else if (ii == 127) fputs("DEL",stderr);
  244. XX   else {
  245. XX      if (ii >= 128) { fputc('M',stderr); fputc('-',stderr); ii -= 128; }
  246. XX      if (ii <  ' ') { fputc('^',stderr); ii += '@'; }
  247. XX      fputc(ii,stderr);
  248. XX   } /* else */
  249. XX} /* fputctl */
  250. XX
  251. XX
  252. XX/*
  253. XX| Abs: Display copyright notice.
  254. XX| Sef: Sets the cright flag FALSE so that we only displays it once.
  255. XX*/
  256. XXvoid showcright()
  257. XX{
  258. XX   fprintf(stderr,"pep  ver. %s; Copyright (c) 1989 Gisle Hannemyr\n",VERSION);
  259. XX   cright = FALSE;
  260. XX} /* showcright */
  261. XX
  262. XX
  263. XX/*
  264. XX| Abs: Display message and abort.
  265. XX*/
  266. XXvoid mess(err)
  267. XXint err;
  268. XX{
  269. XX   if (cright) showcright();
  270. XX   fputs("pep: ",stderr);
  271. XX   switch(err) {
  272. XX      case  1: fputs("incompatible options",       stderr); break;
  273. XX      case  2: fputs("missing '+' or '-'",         stderr); break;
  274. XX      case  3: fputs("bad guard digit",            stderr); break;
  275. XX      case  4: fputs("invalid line terminator",    stderr); break;
  276. XX      case  5: fputs("no more room",               stderr); break;
  277. XX      case  6: fputs("bad conversion table",       stderr); break;
  278. XX      case  7: fputs("no matching files",          stderr); break;
  279. XX      case  8: fputs("cannot pause reading stdin", stderr); break;
  280. XX      case  9: fputs("sorry, not yet implemented", stderr); break;
  281. XX      default: fputs("unknown error",              stderr); break;
  282. XX   } /* switch */
  283. XX   putc('\n',stderr);
  284. XX   exit(ERROR_EXIT);
  285. XX} /* mess */
  286. XX
  287. XX
  288. XXvoid showprogress()
  289. XX{
  290. XX   LCount++;
  291. XX   if (oflago && ((LCount % 64) == 0)) fprintf(stderr,"\r%ld ",LCount);
  292. XX} /* showprogress */
  293. XX
  294. XX
  295. XX/*
  296. XX| Abs: Parse desired line terminator.
  297. XX| Des: r        = CR
  298. XX|      n        = NL
  299. XX|      s        = RS
  300. XX|      #        = CRLF
  301. XX|      -        = none
  302. XX|      <number> = use this as the terminator
  303. XX| Sef: IFrst, ILast, ILimit.
  304. XX*/
  305. XXvoid getterm(ss)
  306. XXchar *ss;
  307. XX{
  308. XX   if       (*ss == 'n')  EndOLn = '\n';
  309. XX   else if  (*ss == 'r')  EndOLn = '\r';
  310. XX   else if  (*ss == 's')  EndOLn =   30;
  311. XX   else if  (*ss == '#')  EndOLn =   -1;
  312. XX   else if  (*ss == '-')  EndOLn =   -2;
  313. XX   else if (isdigit(*ss)) EndOLn = atoi(ss);
  314. XX   else mess(4);
  315. XX} /* getterm */
  316. XX
  317. XX
  318. XX/*
  319. XX| Abs: Check toggle.
  320. XX| Ret: TRUE if toggle is on, else off.
  321. XX*/
  322. XXBOOL swchk(dd)
  323. XXchar dd;
  324. XX{
  325. XX   if ((dd != '+') && (dd != '-')) mess(2);
  326. XX   return(dd == '+');
  327. XX} /* swchk */
  328. XX
  329. XX
  330. XX/*
  331. XX| Abs: Check fold direction and set up direction flags.
  332. XX| Des: IFrst set to 1 if folding to 8 bit character set.
  333. XX| Sef: IFrst, ILast, ILimit.
  334. XX*/
  335. XXvoid folddir(dd)
  336. XXchar dd;
  337. XX{
  338. XX   if (swchk(dd)) { IFrst = 1; ILast = 0; ILimit =  91; }
  339. XX   else           { IFrst = 0; ILast = 1; ILimit = 128; }
  340. XX} /* folddir */
  341. XX
  342. XX
  343. XX/*
  344. XX| Abs: Show transformations.
  345. XX| Imp: Moved from main() coz MS-C don't like big main functions.
  346. XX*/
  347. XXvoid showoptions(tabledir,cname)
  348. XXchar *tabledir, *cname;
  349. XX{
  350. XX   int  cc;
  351. XX
  352. XX   fputs("Transformations:\n",stderr);
  353. XX   if (!vflagv) fputs(" * stripping all trailing spaces;\n",stderr);
  354. XX
  355. XX   if (gflagg) {
  356. XX      fprintf(stderr," * translating using table in file \"%s\";\n",cname);
  357. XX      readtable(tabledir,cname,TRUE);
  358. XX   } /* if gflagg */
  359. XX
  360. XX#ifdef __TURBOC__
  361. XX   fputs(" * preserving file dates;\n",stderr);
  362. XX#endif
  363. XX   if (oflago) fputs(" * output file is input file (not stdout);\n",stderr);
  364. XX   if (backup) fputs(" * creating .BAK copies of input file;\n",stderr);
  365. XX   if (bflagb) fprintf(stderr," * %sing non ASCII-codes;\n", xflagx ? "expand" : "remov");
  366. XX   if (dflagd || iflagi || mflagm) {
  367. XX      char *machine;
  368. XX      if      (dflagd) machine = "DEC";
  369. XX      else if (iflagi) machine = "IBM";
  370. XX      else if (mflagm) machine = "MAC";
  371. XX      if (IFrst) fprintf(stderr," * Norwegian 7-bit ==> 8-bit %s charset", machine);
  372. XX      else       fprintf(stderr," * 8-bit %s charset ==> norwegian 7-bit", machine);
  373. XX      if (kflagk) fputs(",\n   using \\\\ for Norwegian \"OE\";\n",stderr);
  374. XX      else        fputs(";\n",stderr);
  375. XX   } /* if (dflagd || iflagi || mflagm) */
  376. XX   if (sflags) fprintf(stderr," * extracting strings >= %d characters;\n",StrSiz);
  377. XX   if (cflagc && tflagt) {
  378. XX      fputs(" * optimizing out spaces from tabulation;\n",stderr);
  379. XX      if (ITabSz != OTabSz) fprintf(stderr," * repacking tabs, size: %d --> %d;\n",ITabSz,OTabSz);
  380. XX   } else {
  381. XX      if (tflagt) fprintf(stderr," * expanding tabs, size: %d;\n",ITabSz);
  382. XX      if (cflagc) fprintf(stderr," * inserting tabs, size: %d;\n",OTabSz);
  383. XX   }
  384. XX   if (eflage) fputs(" * interpreting ANSI escape sequences;\n",stderr);
  385. XX
  386. XX   if (wflag0) fputs(" * WS doc. mode ==> 7-bit text files;\n",stderr);
  387. XX   if (wflag1) fputs(" * 7-bit text files ==> WS doc. mode;\n",stderr);
  388. XX   else {
  389. XX      if (EndOLn ==  -2 ) fputs(" * removing line terminators",stderr);
  390. XX      else {
  391. XX         if (vflagv) fputs(" * terminating paragraphs only with ",stderr);
  392. XX         else        fputs(" * terminating lines with ",stderr);
  393. XX         if      (EndOLn ==  -1 ) fputs("CRLF",stderr);
  394. XX         else if (EndOLn == '\n') fputs("LF",  stderr);
  395. XX         else if (EndOLn == '\r') fputs("CR",  stderr);
  396. XX         else if (EndOLn ==  30 ) fputs("RS",  stderr);
  397. XX         else fputctl(EndOLn);
  398. XX      } /* if EndOLn != -2 */
  399. XX   } /* if else not WS */
  400. XX#ifdef __VMS__
  401. XX   fputs(" (Stream_LF);\n",stderr);
  402. XX#else
  403. XX   fputs(";\n",stderr);
  404. XX#endif
  405. XX
  406. XX   if (zflagz) fputs(" * zeroing parity bit;\n",stderr);
  407. XX   fprintf(stderr," * %sing control characters.\n", xflagx ? "expand" : "remov");
  408. XX   if (pflagp) {
  409. XX      fputs("\nHit CTRL-C to abort, RETURN continue. ",stderr);
  410. XX      cc = getc(stdin);
  411. XX      if (cc == 3) exit(ERROR_EXIT);   /* Not necessary for cooked microes. */
  412. XX   } /* if */
  413. XX} /* showoptions */
  414. XX
  415. XX
  416. XX/*
  417. XX| Abs: Do a complete file.
  418. XX| Sef: Zero line cont.
  419. XX*/
  420. XXvoid dofile(eflage)
  421. XXint eflage;
  422. XX{
  423. XX#ifndef __VMS__
  424. XX   /* Speed things up using a bigger I/O buffer. */
  425. XX   if (setvbuf(Fdi,NULL,_IOFBF,16384) || setvbuf(Fdo,NULL,_IOFBF,16384))
  426. XX      mess(5); /* No more room */
  427. XX#endif
  428. XX   LCount = 0L;
  429. XX   if (eflage) doansi(guardl); else doplain();
  430. XX} /* dofile */
  431. XX
  432. XX
  433. XX/*---( main )---------------------------------------------------------------*/
  434. XX
  435. XXmain(argc, argv)
  436. XXint  argc;
  437. XXchar **argv;
  438. XX{
  439. XX   struct stat statbuf;
  440. XX   unsigned int statype;
  441. XX   unsigned int statmod;
  442. XX   char *tabledir;
  443. XX   char *cname, *nname, *ss;
  444. XX   int  cc;
  445. XX   char dd;
  446. XX#if __CPM86__ || __MSDOS__
  447. XX   char *cp;
  448. XX   struct DIRLIST *first, *last;
  449. XX#endif
  450. XX#ifdef __TURBOC__
  451. XX   struct ftime *filtim;
  452. XX   int hh;
  453. XX#endif
  454. XX
  455. XX#ifdef __MSDOS__
  456. XX   _fmode = O_BINARY;         /* Tell MS-C, Turbo not to do CRLF expansion. */
  457. XX   tabledir = argv[0];        /* Look for tables in startup-directory.      */
  458. XX#else
  459. XX   tabledir = NULL;           /* No such startup convention for other OS's. */
  460. XX#endif
  461. XX
  462. XX   inittable();
  463. XX
  464. XX   argc--; argv++;           /* skip program name  */
  465. XX   while (argc && (**argv == '-')) {
  466. XX      (*argv)++;             /* skip initial '-'   */
  467. XX      cc = **argv;         /* option letter      */
  468. XX#ifdef __CPM86__
  469. XX      cc = tolower(cc);
  470. XX#endif
  471. XX      (*argv)++;             /* skip option letter */
  472. XX      dd = **argv;           /* arg argument       */
  473. XX      switch (cc) {
  474. XX         case 'a': showcright(); fputs(about1, stderr); fputs(about2, stderr);
  475. XX                   exit(NORML_EXIT);
  476. XX         case 'b': bflagb++; break;
  477. XX         case 'c': cflagc++; if (**argv != '\0') OTabSz = atoi(*argv); break;
  478. XX         case 'd': dflagd++; folddir(dd); break;
  479. XX         case 'e': eflage++; if (**argv != '\0') guardl = atoi(*argv);
  480. XX                   tflagt++;
  481. XX                   break;
  482. XX         case 'g': gflagg++; cname = *argv; break;
  483. XX         /*   'h': OK to fall thru' to default */
  484. XX         case 'i': iflagi++; folddir(dd); break;
  485. XX         case 'k': kflagk++; folddir(dd); break;
  486. XX         case 'm': mflagm++; folddir(dd); break;
  487. XX         case 'o': oflago++; if (dd == 'b') backup++; break;
  488. XX         case 'p': pflagp++; break;
  489. XX         case 's': sflags++; if (**argv != '\0') StrSiz = atoi(*argv); break;
  490. XX         case 't': tflagt++; if (**argv != '\0') ITabSz = atoi(*argv); break;
  491. XX         case 'u': uflagu++; if (**argv != '\0') getterm(*argv); else mess(4); break;
  492. XX         case 'v': vflagv++; break;
  493. XX         case 'w': if (swchk(dd))  tflagt = wflag1 = TRUE;
  494. XX                   else            zflagz = wflag0 = TRUE;
  495. XX                   break;
  496. XX         case 'x': xflagx++; break;
  497. XX         case 'z': zflagz++; break;
  498. XX         default : showcright();
  499. XX               fputs(usage1,stderr); fputs(usage2,stderr);
  500. XX               exit(NORML_EXIT);
  501. XX      } /* switch */
  502. XX      argc--; argv++;
  503. XX   } /* while options */
  504. XX
  505. XX   if (pflagp && !argc) mess(8);
  506. XX   if ((dflagd + gflagg + iflagi + mflagm + zflagz) > 1) mess(1);
  507. XX   if ((guardl < 0) || (guardl > 2)) mess(3);
  508. XX   if (kflagk && !(dflagd + mflagm)) iflagi++;
  509. XX   if (!uflagu && dflagd && IFrst) EndOLn = '\n'; /* To DEC (Dec uses LF as terminator) */
  510. XX   if (!uflagu && iflagi && IFrst) EndOLn = -1;   /* To IBM (Uses  CRLF  as terminator) */
  511. XX   if (!uflagu && mflagm && IFrst) EndOLn = '\r'; /* To Mac (Mac uses CR as terminator) */
  512. XX
  513. XX   Fdi = stdin;              /* Default */
  514. XX   Fdo = stdout;
  515. XX#ifdef __MSDOS__
  516. XX   setmode(fileno(Fdi),_fmode); /* Make sure that even braindamaged  MS-DOS */
  517. XX   setmode(fileno(Fdo),_fmode); /*    are transparent when redirecting i/o. */
  518. XX#endif
  519. XX   if (!argc) { /* Doing standard input */
  520. XX      if      (pflagp) showoptions(tabledir,cname);
  521. XX      else if (gflagg) readtable(tabledir,cname,FALSE);
  522. XX      if      (oflago) mess(7);
  523. XX      dofile(eflage);
  524. XX   } else {
  525. XX      showcright();
  526. XX      showoptions(tabledir,cname);
  527. XX#if __CPM86__ || __MSDOS__
  528. XX      first = expwildcard(argv);
  529. XX      if (!first) mess(7);
  530. XX      /* Inv: first now points to a start of linked list of files. */
  531. XX      last = first;
  532. XX      while (last) {
  533. XX         cname = last->fnam;
  534. XX     if (oflago) {
  535. XX        putc('\r',stderr);
  536. XX        putc('\n',stderr);
  537. XX#else
  538. XX      while (argc) {
  539. XX         cname = *argv;
  540. XX     if (oflago) {
  541. XX        putc('\n',stderr);
  542. XX#endif
  543. XX        fputs(cname,stderr);
  544. XX     } /* if oflago */
  545. XX
  546. XX#ifdef __UNIX__
  547. XX     if (lstat(cname,&statbuf)) {
  548. XX#else
  549. XX     if (stat(cname,&statbuf)) {
  550. XX#endif
  551. XX        if (!oflago) fputs(cname,stderr);
  552. XX            fputs(": can't access\n", stderr);
  553. XX            goto cont;
  554. XX     } /* if not stat */
  555. XX     statmod = statbuf.st_mode;
  556. XX     statype = statbuf.st_mode &  S_IFMT;
  557. XX
  558. XX     if (statype != S_IFREG) {
  559. XX        if (!oflago) fputs(cname,stderr);
  560. XX            if      (statype == S_IFDIR) fputs(": directory\n", stderr);
  561. XX#ifdef __UNIX__
  562. XX            else if (statype == S_IFLNK) fputs(": symbolic link\n", stderr);
  563. XX#endif
  564. XX        else                         fputs(": special file\n", stderr);
  565. XX            goto cont;
  566. XX     } /* if not a regular file */
  567. XX
  568. XX         if ((Fdi = fopen(cname,"r")) == NULL) {
  569. XX        if (!oflago) fputs(cname,stderr);
  570. XX            fputs(": can't open\n", stderr);
  571. XX            goto cont;
  572. XX         }
  573. XX         if (oflago) if ((Fdo = fopen(TEMPFIL,"w")) == NULL) {
  574. XX            fputs(": can't create tmpfile\n",stderr); exit(ERROR_EXIT);
  575. XX         }
  576. XX#ifdef __TURBOC__
  577. XX         if (!(filtim = (struct ftime *)malloc(sizeof(struct ftime))))
  578. XX            mess(5); /* No more room */
  579. XX         getftime(fileno(Fdi),filtim);
  580. XX#endif
  581. XX         if (oflago) putc('\n',stderr);
  582. XX         dofile(eflage);
  583. XX         if (oflago) fprintf(stderr,"\r   Done. %ld lines written.",LCount);
  584. XX
  585. XX         if (ferror(Fdi) || ferror(Fdo)) {
  586. XX            fputs("\npep: I/O error, file unchanged\n", stderr);
  587. XX         } else {
  588. XX            fclose(Fdi);
  589. XX            if (oflago) {
  590. XX           fclose(Fdo);
  591. XX           if (chmod(TEMPFIL,statmod)) fputs("\npep: could not set mode\n",stderr);
  592. XX#ifdef __TURBOC__
  593. XX               if ((hh  = _open(TEMPFIL,O_RDONLY)) != -1) setftime(hh,filtim);
  594. XX               _close(hh);
  595. XX#endif
  596. XX            } /* if (oflago) */
  597. XX
  598. XX            cc = 0; /* error flag */
  599. XX            if (backup) {
  600. XX               if (!(nname = (char *)malloc(strlen(cname)+5))) mess(5);
  601. XX               strcpy(nname,cname);
  602. XX               if (ss = strrchr(nname,'.')) *ss = '\0';
  603. XX               strcat(nname,".BAK");
  604. XX               unlink(nname);
  605. XX#ifdef __UNIX__
  606. XX               if (strcmp(nname,cname)) {  /* UNIX names are case sensitive */
  607. XX#else
  608. XX               if (stricmp(nname,cname)) { /*    other filenames are not.   */
  609. XX#endif
  610. XX                  if (rename(cname,nname)) cc++;
  611. XX               } /* if same names */
  612. XX            } else {
  613. XX               if (oflago) if (unlink(cname)) cc++;
  614. XX            } /* if .. else no backup */
  615. XX            if (oflago) if (rename(TEMPFIL,cname)) cc++;
  616. XX            if (cc) {
  617. XX               fprintf(stderr, "\npep: error creating %s, file is %s\n",cname,TEMPFIL);
  618. XX               exit(ERROR_EXIT);
  619. XX            } /* if error */
  620. XX         } /* if .. else close file */
  621. XXcont:
  622. XX#if __CPM86__ || __MSDOS__
  623. XX         last = last->next;
  624. XX      } /* while */
  625. XX   dispwildcard(first);
  626. XX#else
  627. XX         argc--; argv++;
  628. XX      } /* while */
  629. XX      if (oflago) putc('\n',stderr);
  630. XX#endif
  631. XX   } /* if .. else read named files */
  632. XX   exit(NORML_EXIT);
  633. XX}  /* main */
  634. XX
  635. XX/* EOF */
  636. SHAR_EOF
  637. if test 21491 -ne "`wc -c main.c`"
  638. then
  639. echo shar: error transmitting main.c '(should have been 21491 characters)'
  640. fi
  641. #    End of shell archive
  642. exit 0
  643.  
  644.